package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.CoinAssetsList;
import com.chain.BrotherExchange.data.model.LegalTenderAssetsList;
import com.chain.BrotherExchange.data.model.UserAssets;
import com.chain.BrotherExchange.utils.Androids;

import java.util.List;

/**
 * 我的资产
 */
public class MyAssetsViewModel extends AndroidViewModel {

    private final int uid = Androids.LocalConfig.getUid();
    private final String token = Androids.LocalConfig.getToken();

    private final MutableLiveData<Resource<UserAssets.DataBean>> mUserAssertsLiveData;
    private final MutableLiveData<Resource<List<CoinAssetsList.DataBean.ListBean>>> mCoinAsetsListLiveData;
    private final MutableLiveData<Resource<List<LegalTenderAssetsList.DataBean.ListBean>>> mLegalTenderAssetsListLiveData;

    public MyAssetsViewModel(@NonNull Application application) {
        super(application);
        mUserAssertsLiveData = new MutableLiveData<>();
        mCoinAsetsListLiveData = new MutableLiveData<>();
        mLegalTenderAssetsListLiveData = new MutableLiveData<>();
    }

    // 用户资产
    public MutableLiveData<Resource<UserAssets.DataBean>> requestUserAsserts() {
        if (!Androids.LocalConfig.isLogin()) {
            return mUserAssertsLiveData;
        }

        int uid = Androids.LocalConfig.getUid();
        String token = Androids.LocalConfig.getToken();
        mUserAssertsLiveData.setValue(Resource.loading());

        DataRepository.get().userAssets(token, uid, result -> {
            UserAssets.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                mUserAssertsLiveData.setValue(Resource.success(resultData));
            } else if (result.isNotNetwork()) {
                mUserAssertsLiveData.setValue(Resource.notNetwork());
            } else {
                mUserAssertsLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mUserAssertsLiveData;
    }

    // 币币资产
    public MutableLiveData<Resource<List<CoinAssetsList.DataBean.ListBean>>>
    requestMonetaryAssets() {
        mCoinAsetsListLiveData.setValue(Resource.loading());
        DataRepository.get().coinAssetsList(token, uid, this::coinAssetsList);
        return mCoinAsetsListLiveData;
    }

    // 法币资产
    public MutableLiveData<Resource<List<LegalTenderAssetsList.DataBean.ListBean>>>
    requestLegalCurrencyAssets() {
        mLegalTenderAssetsListLiveData.setValue(Resource.loading());
        DataRepository.get().legalTenderAssetsList(token, uid, this::legalTenderAssets);
        return mLegalTenderAssetsListLiveData;
    }

    private void legalTenderAssets(LegalTenderAssetsList result){
        LegalTenderAssetsList.DataBean resultData = result.getData();
        if (result.isSucceed() && resultData != null) {
            List<LegalTenderAssetsList.DataBean.ListBean> list = resultData.getList();
            if (list != null && !list.isEmpty()) {
                mLegalTenderAssetsListLiveData.setValue(Resource.success(list));
            } else {
                mLegalTenderAssetsListLiveData.postValue(Resource.empty());
            }

        } else if (result.isNotNetwork()) {
            mLegalTenderAssetsListLiveData.setValue(Resource.notNetwork());
        } else if (result.isLogonFailure()) {
            mLegalTenderAssetsListLiveData.setValue(Resource.tokenError(result.getMsg()));
        } else {
            mLegalTenderAssetsListLiveData.setValue(Resource.error(result.getMsg()));
        }
    }

    private void coinAssetsList(CoinAssetsList result) {
        CoinAssetsList.DataBean resultData = result.getData();
        if (result.isSucceed() && resultData != null) {
            List<CoinAssetsList.DataBean.ListBean> list = resultData.getList();
            if (list != null && !list.isEmpty()) {
                mCoinAsetsListLiveData.setValue(Resource.success(list));
            } else {
                mCoinAsetsListLiveData.postValue(Resource.empty());
            }

        } else if (result.isNotNetwork()) {
            mCoinAsetsListLiveData.setValue(Resource.notNetwork());
        } else if (result.isLogonFailure()) {
            mCoinAsetsListLiveData.setValue(Resource.tokenError(result.getMsg()));
        } else {
            mCoinAsetsListLiveData.setValue(Resource.error(result.getMsg()));
        }
    }
}
